package com.zmn.plat.common.util;

import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.StringUtil;

import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author tanbiao
 * @description:提供给渠道的工具类
 * @modifier
 * @since 2021/3/15 10:10
 **/
public class ChannelUtil {

    private final static Pattern ANY_CHAR_PATTERN = Pattern.compile("[\\s\\S]");

    private final static String DEFAULT_HIDER = "*";

    /**
    * 描述：对字符串进行隐藏显示
    * @param orgStr：原始字符串
     * @param minLength:字符串最小长度，小于该长度全部隐藏
    * @param leftShowLetters：左侧能显示的字符串
    * @param rightShowLetters：右侧能显示的字符串
    * @param hider：隐藏字符串的填充
    * @author tanbiao
    * @since 2021/3/15 10:15
    * @return java.lang.String
    * @modifier      
    */
    public static String hideString(String orgStr,Integer minLength,Integer leftShowLetters,Integer rightShowLetters,String hider){
        if(StringUtil.isBlank(orgStr)){
            return orgStr;
        }
        if(!NumberUtil.isPositiveInteger(minLength)||!NumberUtil.isPositiveInteger(leftShowLetters)||!NumberUtil.isPositiveInteger(rightShowLetters)){
            minLength=8;
            leftShowLetters = 4;
            rightShowLetters=4;
        }
        if(StringUtil.isBlank(hider)){
            hider = DEFAULT_HIDER;
        }

        // 不足最小长度，全部隐藏
        if(orgStr.length()<=minLength){
            Matcher m = ANY_CHAR_PATTERN.matcher(orgStr);

            return m.replaceAll(hider);
        }

        //满足长度的对中间的字符串进行替换即可
        Integer hideLength = orgStr.length()-leftShowLetters-rightShowLetters;
        String regx = "([\\s\\S]{"+leftShowLetters+"})[\\s\\S]{"+hideLength+"}([\\s\\S]{"+rightShowLetters+"})";

        orgStr = orgStr.replaceAll(regx, "$1"+getHiderBylength(hideLength,hider)+"$2");

        return orgStr;
    }

    private static String getHiderBylength(Integer length,String hider){
        StringBuilder stringBuilder = new StringBuilder("");
        if(NumberUtil.isPositiveInteger(length)){
            for(int i=0;i<length;i++){
                stringBuilder.append(hider);
            }
        }
        return stringBuilder.toString();
    }

    public static Boolean checked(Integer channelId, List<Integer> channels) {
        if (CollectionUtil.isNotNullOrEmpty(channels) && Objects.nonNull(channelId)) {
            return channels.contains(channelId);
        }
        return Boolean.FALSE;
    }


    public static void main(String[] args) {
        String orgStr = "132023110";
        System.out.println(hideString(orgStr,null,null,null,null));
    }
}
